<template>
  <div class="login-container">
    <!-- 左侧图片区域 -->
    <div class="login-left">
      <div class="image-content">
        <div class="welcome-text">
          <h2>欢迎使用社区智慧管理系统</h2>
          <p>高效管理社区环境</p>
        </div>
      </div>
    </div>

    <!-- 右侧登录表单区域 -->
    <div class="login-right">
      <div class="login-form-wrapper">
        <div class="login-form">
          <h2 class="login-title">管理员登录</h2>
          <p class="login-subtitle">请输入您的账号和密码</p>

          <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef">
            <el-form-item prop="username">
              <el-input
                  v-model="loginForm.account"
                  placeholder="请输入用户名"
                  prefix-icon="User"
                  size="large"
                  class="custom-input"
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                  v-model="loginForm.password"
                  type="password"
                  placeholder="请输入密码"
                  prefix-icon="Lock"
                  size="large"
                  show-password
                  class="custom-input"
              />
            </el-form-item>

            <el-form-item>
              <el-button
                  type="primary"
                  class="login-btn"
                  size="large"
                  @click="login"
                  :loading="loading"
              >
                {{ loading ? '登录中...' : '登录' }}
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import axios from "axios";
import router from "@/router";

// 登录表单数据
const loginForm = ref({
  account: '',
  password: ''
})

// 加载状态
const loading = ref(false)

// 表单验证规则
const loginRules = reactive({
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ]
})

const login = () => {
  loading.value = true
  axios.post(BASE_URL + '/api/users/login',loginForm.value)
      .then((response) => {
        console.log(response.data)
        if(response.data.state === 2001){
          ElMessage.success('登录成功');


          localStorage.setItem('auth',JSON.stringify(response.data.data));
          router.push('/');
        }else {
          ElMessage.error("用户登录失败，请检查后重试");
          loginForm.value = {account:'',password:''}
        }
      })
      .finally(() => {
        loading.value = false
      });
}
</script>

<style scoped>
.login-container {
  display: flex;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  position: relative;
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #3c82f5 0%, #2a56c6 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.login-left::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url(../../public/imgs/login/img.png) center/cover no-repeat;
  opacity: 0.85;
}

.image-content {
  position: relative;
  z-index: 2;
  text-align: center;
  color: white;
  padding: 0 20px;
}

.welcome-text h2 {
  font-size: 32px;
  font-weight: 600;
  margin-bottom: 16px;
  letter-spacing: 1px;
}

.welcome-text p {
  font-size: 18px;
  opacity: 0.9;
  font-weight: 300;
}

.login-right {
  width: 42%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: ghostwhite;
  backdrop-filter: blur(10px);
  position: relative;
  z-index: 10;
  box-shadow: -5px 0 15px rgba(0, 0, 0, 0.05);
}

.login-form-wrapper {
  width: 100%;
  max-width: 400px;
  padding: 0 30px;
}

.login-form {
  padding: 40px 35px;
  border-radius: 12px;
  background: white;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.08);
}

.login-title {
  text-align: center;
  margin-bottom: 10px;
  color: #303133;
  font-weight: 600;
  font-size: 28px;
  letter-spacing: 1px;
}

.login-subtitle {
  text-align: center;
  margin-bottom: 40px;
  color: #606266;
  font-size: 15px;
}

:deep(.custom-input .el-input__wrapper) {
  border-radius: 8px;
  padding: 4px 15px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}

:deep(.custom-input .el-input__wrapper:hover),
:deep(.custom-input .el-input__wrapper.is-focus) {
  box-shadow: 0 2px 10px rgba(60, 130, 245, 0.25);
}

:deep(.custom-input .el-input__inner) {
  padding: 12px 5px;
  font-size: 15px;
}

:deep(.el-form-item) {
  margin-bottom: 26px;
}

.login-btn {
  width: 100%;
  border-radius: 8px;
  font-size: 16px;
  height: 48px;
  margin-top: 10px;
  font-weight: 500;
  letter-spacing: 1px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 10px rgba(60, 130, 245, 0.3);
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 15px rgba(60, 130, 245, 0.4);
}

/* 响应式设计 */
@media (max-width: 992px) {
  .login-left {
    display: none;
  }

  .login-right {
    width: 100%;
    background: white;
  }

  .login-form-wrapper {
    padding: 0 20px;
  }

  .login-form {
    padding: 30px 25px;
  }
}

/* 动画效果 */
.login-form {
  animation: fadeIn 0.6s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>